package org.bigk.invoices.model;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.math.NumberUtils;

@Entity
@Table(name = "invoices")
public class Invoice extends PaginatedBean implements Serializable, Cloneable {

	private static final long serialVersionUID = 628105541187276617L;

	@Id
	@GeneratedValue
	@Column(name = "id")
	private Long id;

	@Column(name = "year")
	private String year;

	@Column(name = "number")
	private Long number;

	@Column(name = "document_date")
	private Date documentDate;

	@Column(name = "sold_date")
	private Date soldDate;

	@Column(name = "payment_date")
	private Date paymentDate;

	@Column(name = "payment_kind_id")
	private Long paymentKindId;

	@ManyToOne
	@JoinColumn(name = "payment_kind_id", insertable = false, updatable = false)
	private PaymentKind paymentKind;

	@OneToMany(mappedBy = "invoiceId", cascade = CascadeType.ALL)
	private List<InvoicePosition> invoicePositions;

	@OneToMany(mappedBy = "invoiceId", cascade = CascadeType.ALL)
	private List<InvoicePurchaser> invoicePurchasers;

	@Transient
	private Map<Double, InvoicePositionSummary> calculatedSummaries;

	@Transient
	private List<Double> calculatedSummariesKeys;

	@Transient
	private String hrInvoiceNumber;

	@Transient
	private String hrTotalIntegerPart;

	@Transient
	private String hrTotalDigitsPart;

	public Invoice() {
	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public Invoice clone() throws CloneNotSupportedException {
		return (Invoice) super.clone();
	}

	public Date getDocumentDate() {
		return documentDate;
	}

	public void setDocumentDate(Date documentDate) {
		this.documentDate = documentDate;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getNumber() {
		return number;
	}

	public void setNumber(Long number) {
		this.number = number;
	}

	public Long getPaymentKindId() {
		return paymentKindId;
	}

	public void setPaymentKindId(Long paymentKindId) {
		this.paymentKindId = paymentKindId;
	}

	public PaymentKind getPaymentKind() {
		return paymentKind;
	}

	public void setPaymentKind(PaymentKind paymentKind) {
		this.paymentKind = paymentKind;
	}

	public Date getSoldDate() {
		return soldDate;
	}

	public void setSoldDate(Date soldDate) {
		this.soldDate = soldDate;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public Date getPaymentDate() {
		return paymentDate;
	}

	public void setPaymentDate(Date paymentDate) {
		this.paymentDate = paymentDate;
	}

	public List<InvoicePosition> getInvoicePositions() {
		return invoicePositions;
	}

	public void setInvoicePositions(List<InvoicePosition> invoicePositions) {
		this.invoicePositions = invoicePositions;
	}

	public List<InvoicePurchaser> getInvoicePurchasers() {
		return invoicePurchasers;
	}

	public void setInvoicePurchasers(List<InvoicePurchaser> invoicePurchasers) {
		this.invoicePurchasers = invoicePurchasers;
	}

	public Map<Double, InvoicePositionSummary> getCalculatedSummaries() {
		return calculatedSummaries;
	}

	public void setCalculatedSummaries(
			Map<Double, InvoicePositionSummary> calculatedSummaries) {
		this.calculatedSummaries = calculatedSummaries;
	}

	public List<Double> getCalculatedSummariesKeys() {
		return calculatedSummariesKeys;
	}

	public void setCalculatedSummariesKeys(List<Double> calculatedSummariesKeys) {
		this.calculatedSummariesKeys = calculatedSummariesKeys;
	}

	public BigDecimal getTotal() {
		return calculatedSummaries.get(NumberUtils.DOUBLE_ONE).getTotal();
	}

	public BigDecimal getSum() {
		return calculatedSummaries.get(NumberUtils.DOUBLE_ONE).getSum();
	}

	public String getHrInvoiceNumber() {
		return hrInvoiceNumber;
	}

	public void setHrInvoiceNumber(String hrInvoiceNumber) {
		this.hrInvoiceNumber = hrInvoiceNumber;
	}

	public String getHrTotalIntegerPart() {
		return hrTotalIntegerPart;
	}

	public void setHrTotalIntegerPart(String hrTotalIntegerPart) {
		this.hrTotalIntegerPart = hrTotalIntegerPart;
	}

	public String getHrTotalDigitsPart() {
		return hrTotalDigitsPart;
	}

	public void setHrTotalDigitsPart(String hrTotalDigitsPart) {
		this.hrTotalDigitsPart = hrTotalDigitsPart;
	}
}
